/* 选择时间和价格<为了后期扩展性，写成异步>
* @Author: Jerry-chen
* @Date:   2014-07-16 17:08:56
* @Last Modified by:   Administrator
* @Last Modified time: 2014-07-31 09:58:15
*/

 		$(function(){
 			// alert('sfd');
            $('.ajax-bottom').click(function(){

               //清除HTML代码
        	   $('.choose-number').empty();
                  //获得当前选择的数据
                  //商品ID
                  	
            		var goodsid=$('.choose-time .over').attr('ajaxgoodsid');
                    var time = $('.choose-time .over').attr('ajaxtime');
                    var timeid=$('.choose-time .over').attr('ajaxtimeid');
                    //获得ID，方便后面减库存
                    var goodsprice =$(this).attr('ajaxgoodsprice');
                    var price = $(this).attr('ajaxprice');
                 $.ajax({
                        url: APP + '?c=Goods&m=ajax_choose',
                        type: 'post',
                        dataType: 'json',
                        data: {'time' : time,'price':price,'timeid':timeid,'goodsprice':goodsprice,'goodsid':goodsid},
                        success:function(data){
                            //定义属性字符串
                            var chooseprice = "";
                            var shopcar = "";
                            // chooseprice +="<span>-"++"</span><span>-""</span><span class='add'><u>数量</u><a href='javascript:;' class='add'>+</a><input type='' value='1' /><a href='javascript:;'>-</a></span><input type='hidden' name='time' value='"+data.time+"' /> <input type='hidden' name='price' value='"+data.price+"' />";
                             chooseprice+="<span class='word'>您选择了:</span>";
                            chooseprice+="<span>'"+data.time+" '</span>";
                             chooseprice+="<span>'"+data.price+"元'</span>";
                             chooseprice+="<form class='pricetime' action='"+APP+"?c=Straightbuy&m=index' method='post'> <span class='add'><u>数量</u><a href='javascript:;'>+</a><input type='text'class='num' style='width:20px;' name='number' value='1' /><a href='javascript:;' class='reduce'>-</a></span>";
                             chooseprice+="<input type='hidden' name='time' value='"+data.time+"' /> <input type='hidden' name='price' value='"+data.price+"' /> <input type='hidden' name='timeid' value='"+data.timeid+"' /> <input type='hidden' name='goodsprice' value='"+data.goodsprice+"' /><input type='hidden' name='goodsid' value='"+data.goodsid+"' /></form>";
                             // '<from action="'+ "U:'" + 'index/index' + "'" + '">';
                             // shopcar FORM数据     
                        chooseprice+="<form class='shopcarform' action='"+APP+"?c=Shopcar&m=index' method='post'>";
                        chooseprice+="<input type='hidden' name='time' value='"+data.time+"' /> <input type='hidden' name='shopcarnum' class='shopcarnum' value='1' /><input type='hidden' name='price' value='"+data.price+"' /> <input type='hidden' name='timeid' value='"+data.timeid+"' /> <input type='hidden' name='goodsprice' value='"+data.goodsprice+"' /><input type='hidden' name='goodsid' value='"+data.goodsid+"' /><input type='hidden' name='straight' value='1' /></form>";
                             //指定A标签触发FROM,立即购买
                           	$('.buy-now').click(function() {
                           		// alert(chooseprice);
                           		$('.pricetime').submit()
                           		});
                       
          						 //指定A标签触发FROM,加入购物车
          						 	$('.shopcar').click(function() {
                                     		// alert('fdsajl');
                                     		$('.shopcarform').submit();
                                     		});


                                      //JS增加节点
                                      $('.choose-number').append(chooseprice);    
               		   }
          		  })
         		 });


	  /*添加数量的方法*/	
		var add,reduce,num,num_txt;
		   add=$(".add");//添加数量
		   reduce=$(".reduce");//减少数量
		   num="";//数量初始值
		   num_txt=$(".num");//接受数量的文本框   
		   //var num_val=num_txt.val();//给文本框附上初始值 
		//商品数量增加
		$('.add').live('click', function() {
			// alert('d');
			 num = $(".num").val();  
			 // alert(num);  
		     num++;
		     $(".num").val(num);
         $(".shopcarnum").val(num);
			});
		 /*减少数量的方法*/ 
		$('.reduce').live('click', function() {
			// alert('d');
			  num = $(".num").val();
			   //如果文本框的值大于0才执行减去方法
		 if(num >0)
		 {
		 		num--;
		      //并且当文本框的值为1的时候，减去后文本框直接清空值，不显示0
		      if(num==1)
		      { 
		        $(".num").val("");
		      }
		      //否则就执行减减方法
		      else
		      {
		       num--;
		       $(".num").val(num);
           $(".shopcarnum").val(num);
		      }
		 }
		});

 });
